package com.hzf.ucenter.rpc.service.impl;

import com.hzf.common.util.EmptyUtil;
import com.hzf.ucenter.dao.mapper.*;
import com.hzf.ucenter.dao.model.*;
import com.hzf.ucenter.rpc.api.UcenterApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UcenterApiServiceImpl implements UcenterApiService {
    @Autowired
    UcenterUserOauthMapper ucenterUserOauthMapper;
    @Autowired
    UcenterUserMapper ucenterUserMapper;

    @Autowired
    UcenterUserAuthMapper ucenterUserAuthMapper;
    @Autowired
    UcenterAuthBusinessMapper ucenterAuthBusinessMapper;
    @Autowired
    UcenterAuthTypeMapper ucenterAuthTypeMapper;
    @Autowired
    UcenterAuthInfoMapper ucenterAuthInfoMapper;
    @Autowired
    UcenterUserCityMapper ucenterUserCityMapper;

    /**
     * 接口保存微信第三方用户
     */
    @Override
    public UcenterUserOauth insertOauth(UcenterUserOauth ucenterUserOauth) {
        UcenterUserOauthExample ucenterUserOauthExample = new UcenterUserOauthExample();
        ucenterUserOauthExample.createCriteria()
                .andOpenIdEqualTo(ucenterUserOauth.getOpenId());
        long count = ucenterUserOauthMapper.countByExample(ucenterUserOauthExample);
        if (count > 0) {
            return null;
        }
        ucenterUserOauthMapper.insert(ucenterUserOauth);
        return ucenterUserOauth;
    }

    @Override
    public UcenterUserOauth slectOauth(String openid) {

        return ucenterUserOauthMapper.selectByOpenId(openid);
    }

    @Override
    public UcenterUser insertUser(UcenterUser ucenterUser) {
        UcenterUserExample ucenterUserExample = new UcenterUserExample();
        ucenterUserExample.createCriteria()
                .andUserThirdIdEqualTo(ucenterUser.getUserThirdId());
        long count = ucenterUserMapper.countByExample(ucenterUserExample);
        if (count > 0) {
            return null;
        }
        ucenterUserMapper.insert(ucenterUser);
        return ucenterUser;
    }

    @Override
    public UcenterUser selectUser(int userThirdId) {

        return ucenterUserMapper.selectByUserThirdId(userThirdId);
    }


    @Override
    public UcenterUserAuth insertUserAuth(UcenterUserAuth ucenterUserAuth, int typeStatus) {
        UcenterUserAuthExample ucenterUserAuthExample = new UcenterUserAuthExample();
        ucenterUserAuthExample.createCriteria().andUserIdEqualTo(ucenterUserAuth.getUserId()).andAuthTypeEqualTo(typeStatus);
        List<UcenterUserAuth> list = ucenterUserAuthMapper.selectByExample(ucenterUserAuthExample);

        for (UcenterUserAuth ucenterUserAuth1 : list) {
            if (ucenterUserAuth1 == null) {
            } else {
                if (ucenterUserAuth1.getUcenterAuthType() != null) {
                    if (null != ucenterUserAuth1.getUcenterAuthType().getAuthStatus()) {
                        if (ucenterUserAuth1.getUcenterAuthType().getAuthStatus() != 2) {
                            return null;
                        }
                    }
                }
            }
        }


        UcenterAuthInfoExample ucenterAuthInfoExample = new UcenterAuthInfoExample();
        ucenterAuthInfoExample.createCriteria().andUserIdEqualTo(ucenterUserAuth.getUserId()).andAuthTypeEqualTo(ucenterUserAuth.getAuthType());
        long count = ucenterAuthInfoMapper.countByExample(ucenterAuthInfoExample);
        if (count > 0) {
            return null;
        }
        ucenterUserAuthMapper.insert(ucenterUserAuth);

        return ucenterUserAuth;
    }

    @Override
    public UcenterAuthBusiness insertUserAuthBusiness(UcenterAuthBusiness ucenterAuthBusiness) {
        UcenterAuthInfoExample ucenterAuthInfoExample = new UcenterAuthInfoExample();
        ucenterAuthInfoExample.createCriteria().andUserIdEqualTo(ucenterAuthBusiness.getUserId()).andAuthTypeEqualTo(4);
        long count = ucenterAuthInfoMapper.countByExample(ucenterAuthInfoExample);
        if (count > 0) {
            return null;
        }
        ucenterAuthBusinessMapper.insert(ucenterAuthBusiness);
        return ucenterAuthBusiness;
    }

    @Override
    public UcenterAuthType insetUcenterAuthType(UcenterAuthType ucenterAuthType) {
        ucenterAuthTypeMapper.insert(ucenterAuthType);
        return ucenterAuthType;
    }

    @Override
    public UcenterAuthInfo insetUcenterAuthInfo(UcenterAuthInfo ucenterAuthInfo) {
        UcenterAuthInfoExample ucenterAuthInfoExample = new UcenterAuthInfoExample();
        ucenterAuthInfoExample.createCriteria().andUserIdEqualTo(ucenterAuthInfo.getUserId()).andAuthTypeEqualTo(ucenterAuthInfo.getAuthType());
        long count = ucenterAuthInfoMapper.countByExample(ucenterAuthInfoExample);
        if (count > 0) {
            return null;
        }
        ucenterAuthInfoMapper.insert(ucenterAuthInfo);
        return ucenterAuthInfo;
    }

    @Override
    public List<UcenterUserAuth> selectUcenterUserAuth(UcenterUserAuthExample ucenterUserAuthExample) {

        return ucenterUserAuthMapper.selectByExample(ucenterUserAuthExample);
    }

    @Override
    public List<UcenterAuthInfo> selectUcenterAuthInfo(UcenterAuthInfoExample ucenterAuthInfoExample) {
        return ucenterAuthInfoMapper.selectByExample(ucenterAuthInfoExample);

    }

    @Override
    public int update(UcenterUser ucenterUser) {
        return ucenterUserMapper.updateByPrimaryKey(ucenterUser);
    }

    @Override
    public UcenterUser selectRegionUser(int id, int state) {
        return ucenterUserMapper.selectRegionUser(id, state);
    }

    @Override
    public int insertCity(UcenterUserCity ucenterUserCity) {

        return ucenterUserCityMapper.insert(ucenterUserCity);
    }

    @Override
    public UcenterUserOauth selectOuthEx(UcenterUserOauthExample example) {
        List<UcenterUserOauth> list = ucenterUserOauthMapper.selectByExample(example);
        return list.get(0);
    }

    @Override
    public List<UcenterUserCity> selectUserCity(UcenterUserCityExample ucenterUserCityExample) {

        return ucenterUserCityMapper.selectByExample(ucenterUserCityExample);
    }

    @Override
    public UcenterUserCity getUserCity(UcenterUserCityExample ucenterUserCityExample) {
        List<UcenterUserCity> list = ucenterUserCityMapper.selectByExample(ucenterUserCityExample);
        if (null != list&&0!=list.size()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public UcenterUser getUser(UcenterUserExample example) {
        List<UcenterUser> list= ucenterUserMapper.selectByExample(example);
        if(null!=list&&0!=list.size()){
            return list.get(0);
        }
        return null;
    }

    @Override
    public int updateCity(UcenterUserCity ucenterUserCity) {
        return ucenterUserCityMapper.updateByPrimaryKey(ucenterUserCity);
    }
}
